package lc.q101_150;

import java.util.HashSet;
import java.util.Set;

public class Q142 {

    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode slow = head;
        ListNode quick = head;
        do {
            slow = slow.next;
            if (quick.next == null || quick.next.next == null) {
                return null;
            }
            quick = quick.next;
            quick = quick.next;
        } while (slow != quick);

        slow = head;
        while (slow != quick) {
            slow = slow.next;
            quick = quick.next;
        }
        return slow;
    }

}


